package cn.gwm.flink.streaming.dws.reservecharge;

import cn.gwm.flink.streaming.constant.BaseFields;
import cn.gwm.flink.streaming.dwm.charge.util.SupplyUtil;
import cn.hutool.core.util.ObjectUtil;
import org.apache.flink.api.common.functions.RichMapFunction;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @Author Liu.Nanfang
 * @Date 2023/5/19 9:39
 * @Description
 */
public class ReserveChargeGpsMapFunction extends RichMapFunction<ReserveChargeData, ReserveChargeData> {

    @Override
    public ReserveChargeData map(ReserveChargeData in) throws Exception {
        return queryGpsByWin(in);
    }

    /**
     * 异步获取经纬度信息
     *
     * @param in
     * @return
     */
    private ReserveChargeData queryGpsByWin(ReserveChargeData in) {
        if (ObjectUtil.isNotNull(in.getAdCode()) && !"[]".equals(in.getAdCode()) && !BaseFields.defaultVal.equals(in.getAdCode())) {
            return in;
        }
        if (ObjectUtil.isNotNull(in.getLatitude()) && ObjectUtil.isNotNull(in.getLongitude())) {
            if (!BaseFields.defaultVal.equals(in.getLatitude().toString()) &&
                    !BaseFields.defaultVal.equals(in.getLongitude().toString())) {
                return in;
            }
        }

        Map<String, BigDecimal> gps = SupplyUtil.getGpsByVinAndTid(in.getDeviceId(), in.getItem_time() - 5000L, in.getItem_time() + 5000L);
        if (ObjectUtil.isNotEmpty(gps)) {
            in.setLatitude(gps.get("latitude"));
            in.setLongitude(gps.get("longitude"));
        }


        return in;

    }
}
